<%--
  Created by IntelliJ IDEA.
  User: bin.liu
  Date: 2018/12/5
  Time: 11:11
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<jsp:include page="/pages/sysIndex/default.jsp"/>
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
<style>
    .differentFlag {
        color: #FF0000;
    }

    .differentFlag1 {
        color: #0d8ddb;
    }
</style>

<div class="main-content">
    <div class="box box-main">
        <div class="box-header">
            <div class="box-title">
                <i class="fa icon-trophy"></i> 维修单管理
            </div>
            <div class="box-tools pull-right">
                <a href="#" class="btn btn-default" id="btnSearch" title="查询"><i class="fa fa-filter"></i> 查询</a>
                <sec:authorize access="hasRole('ROLE_ENTERPRISE_REPAIR_ORDER_EDIT')">
                    <a href="javascript:;" onclick="vLayuiUtil.openLayerWindow('wRepairOrder','<c:url
                            value="/web/admin/repairOrder/editPage.action"/>','新增',['80%', '95%'])"
                       class="btn btn-default btnTool" title="新增维修单"><i class="fa fa-plus"></i> 新增</a>
                    <%--<a href="javascript:;" onclick="vLayuiUtil.openLayerWindow('wRepairOrder','<c:url--%>
                    <%--value="/web/admin/repairOrder/editPage400.action"/>','400新增',['80%', '95%'])"--%>
                    <%--class="btn btn-default btnTool" title="400新增维修单"><i class="fa fa-plus"></i> 400新增</a>--%>
                    <a href="javascript:;" onclick="assign(0)"

                       class="btn btn-default btnTool" title="指定接单人"><i class="fa fa-plus"></i> 指定接单人</a>
                    <a href="javascript:;" onclick="assign(1)"
                       class="btn btn-default btnTool" title="指定接单人"><i class="fa fa-plus"></i> 指定岗位</a>
                    <a href="javascript:;" onclick="downloadData()" class="btn btn-default btnTool" title="导出"><i
                            class="fa fa-download"></i> 导出</a>
                </sec:authorize>
            </div>
        </div>

        <div class="box-body">
            <form id="searchForm" action="<c:url value="/web/admin/repairOrder/listJson.action"/>" method="post"
                  class="form-inline hide" data-page-no="1" data-page-size="20" data-order-by="">
                <div class="form-group">
                    <label class="control-label">报修编号：</label>
                    <div class="control-inline">
                        <input type="text" id="reportCode" name="reportCode" value="" maxlength="64"
                               style="width: 200px"
                               class="form-control">
                    </div>
                </div>
                <div class="form-group">
                    <label class="control-label">报修内容：</label>
                    <div class="control-inline">
                        <input type="text" id="reportContent" name="reportContent" value="" maxlength="64"
                               style="width: 200px"
                               class="form-control">
                    </div>
                </div>
                <div class="form-group">
                    <label class="control-label">报修区域：</label>
                    <div class="control-inline">
                        <input type="text" id="reportArea" name="reportArea" value="" maxlength="64"
                               style="width: 200px"
                               class="form-control">
                    </div>
                </div>

                <div class="form-group">
                    <label class="control-label">工单状态：</label>
                    <div class="control-inline" style="width:200px">
                        <select id="orderStatus" name="orderStatus" class="form-control">
                            <option value="" selected>&nbsp;全部&nbsp;&nbsp;&nbsp;</option>
                            <option value="WAIT_TAKING">&nbsp;待接单&nbsp;</option>
                            <option value="WAIT_PRESENCE">&nbsp;待到场&nbsp;</option>
                            <option value="WAIT_CONFIRM_FAULT">&nbsp;待确认故障&nbsp;</option>
                            <option value="UNFINISHED">&nbsp;待完成&nbsp;</option>
                            <option value="IN_AUDITING">&nbsp;审批中&nbsp;</option>
                            <option value="COMPLETED">&nbsp;已完成&nbsp;</option>
                        </select>
                    </div>
                </div>
                <c:if test="${enterprise.enterpriseName!='400center'}">
                    <div class="form-group">
                        <label class="control-label">报修类型：</label>
                        <div class="control-inline" style="width:200px">
                            <select id="reportType" name="reportType" class="form-control">
                                <option value="" selected>&nbsp;全部&nbsp;&nbsp;&nbsp;</option>
                                <option value="COMMON_ORDER">&nbsp;常规报修&nbsp;&nbsp;&nbsp;</option>
                                <option value="FACILITY_ORDER">&nbsp;设备报修&nbsp;&nbsp;&nbsp;</option>
                                <option value="_400_ORDER">&nbsp;400报修&nbsp;&nbsp;&nbsp;</option>
                            </select>
                        </div>
                    </div>
                </c:if>
                <div class="form-group">
                    <label class="control-label">报&nbsp;&nbsp;修&nbsp;&nbsp;人：</label>
                    <div class="control-inline">
                        <input type="text" id="reportUser" name="reportUser" value="" maxlength="64"
                               style="width: 200px"
                               class="form-control">
                    </div>
                </div>
                <div class="form-group">
                    <label class="control-label">客户报修：</label>
                    <div class="control-inline">
                        <input type="text" id="customerName" name="customerName" value="" maxlength="64"
                               style="width: 200px"
                               class="form-control">
                    </div>
                </div>
                <div class="form-group">
                    <label class="control-label">报修电话：</label>
                    <div class="control-inline">
                        <input type="text" id="call" name="call" value="" maxlength="64"
                               style="width: 200px"
                               class="form-control">
                    </div>
                </div>
                <div class="form-group">
                    <label class="control-label">工单类型：</label>
                    <div class="control-inline">
                        <input type="text" id="repairType" name="repairType" value="" maxlength="64"
                               style="width: 200px"
                               class="form-control">
                    </div>
                </div>
                <c:if test="${enterprise.enterpriseName=='400center'}">
                    <div class="form-group">
                        <label class="control-label">所属企业：</label>
                        <div class="control-inline" style="width:200px">
                            <select class="form-control" id="belongEnterprise" name="belongEnterprise">
                                <
                                <option value="">全部</option>
                                <c:forEach items="${enterprises}" var="enter">
                                    <<option value="${enter.id}">${enter.enterpriseName}</option>
                                </c:forEach>
                            </select>
                        </div>

                    </div>
                </c:if>
                <div class="form-group">
                    <label class="control-label">报修时间：</label>
                    <div class="control-inline">
                        <input type="text" id="createDate1" name="createDate1" value="" maxlength="64"
                               autocomplete="off"
                               style="width: 200px"
                               class="form-control">-
                        <input type="text" id="createDate2" name="createDate2" value="" maxlength="64"
                               autocomplete="off"
                               style="width: 200px"
                               class="form-control">
                    </div>
                </div>
                <div class="form-group">
                    <button type="submit" class="btn btn-primary btn-sm">查询</button>
                    <button type="reset" class="btn btn-default btn-sm">重置</button>
                </div>
            </form>

            <div style="overflow-x:auto">
                <table id="dataGrid"></table>
            </div>
            <div id="dataGridPage"></div>
        </div>
    </div>
</div>
<script>
    function refresh(){
        $('#dataGrid').dataGrid('refresh');
    }
    setInterval(refresh, 60000);
    var selectIds = [];

    $('#dataGrid').dataGrid({
        shrinkToFit: false,
        autoScroll: true,
        showCheckbox: true,
        searchForm: $("#searchForm"),
        columnModel: [
            <sec:authorize access="hasRole('ROLE_ENTERPRISE_REPAIR_ORDER_EDIT')">
            {
                header: '操作',
                name: 'actions',
                width: 230,
                frozen: false,
                sortable: false,
                title: false,
                formatter: function (val, obj, row, act) {
                    var a = '<a class="btn btn-default btn-xs btnList" onclick="detail(' + row.id + ')" title="详情"><i class="fa fa-pencil">详情</i></a>';
                    if ("已完成" != row.orderStatus && "审批中" != row.orderStatus  && "已取消" != row.orderStatus) {
                    a += ' <a  class="btn btn-default btn-xs btnList" onclick="editRepairOrder(' + row.id + ')" title="编辑"><i class="fa fa-pencil"></i>编辑</a>&nbsp;';
                    }
                    a += '<a  class="btn btn-default btn-xs btnList" onclick="log(' + row.id + ')" title="日志"><i class="fa fa-file-text-o"></i>日志</a>&nbsp;';
                    <sec:authorize access="hasRole('ROLE_ENTERPRISE_REPAIR_ORDER_DELETE')">
                    a += '<a href="javascript:;" class="btn btn-default btn-xs btnList" onclick=" submitRemove(\'' + row.id + '\', \'0\')"><i class="fa fa-trash-o"></i>删除</a>&nbsp;';
                    </sec:authorize>
                    return a;
                }
            },
            </sec:authorize>
            {header: '主键id', name: 'id', index: 'id', width: 100, align: "center", edittype: "checkbox"},
            {header: '报修编号', name: 'reportCode', index: 'reportCode', width: 250, align: "center"},
            {
                header: '所属企业',
                name: 'enterpriseName',
                index: 'enterpriseName',
                width: 150,
                align: "center",
                sortable: false
            },
            {header: '工单状态', name: 'orderStatus', index: 'orderStatus', width: 150, align: "center", sortable: false},
            {
                header: '报修单类型',
                name: 'repairOrderType',
                index: 'repairOrderType',
                width: 150,
                align: "center",
                sortable: false
            },
            {header: '报修专业', name: 'majorName', index: 'majorName', width: 100, align: "center"},
            {header: '报修等级', name: 'reportLevel', index: 'reportLevel', width: 100, align: "center"},
            {
                header: '报修来源',
                name: 'repairSourceEnum',
                index: 'repairSourceEnum',
                width: 150,
                align: "center",
                sortable: false
            },
            {header: '报修区域', name: 'reportArea', index: 'reportArea', width: 150, align: "center", sortable: false},
            {header: '报修地址', name: 'reportAddress', index: 'reportAddress', width: 200, align: "center"},
            {
                header: '报修内容',
                name: 'reportContent',
                index: 'reportContent',
                width: 200,
                align: "center",
                sortable: false
            },
            {
                header: '客户报修人',
                name: 'customer',
                index: 'customer',
                width: 150,
                align: "center",
                sortable: false
            },
            {
                header: '客户报修电话',
                name: 'callNumber',
                index: 'callNumber',
                width: 150,
                align: "center",
                sortable: false
            },
            {
                header: '报修人',
                name: 'reportUserName',
                index: 'reportUserName',
                width: 150,
                align: "center",
                sortable: false
            },
            {header: '报修时间', name: 'createDate', index: 'createDate', width: 150, align: "center"},
            {
                header: '语音', name: 'word', index: 'word', width: 320, align: 'center',
                formatter: function (val, row) {
                    if ($.trim(val) === "") {
                        return ' — ';
                    } else {
                        return '<audio src="' + val + '" controls="controls" width="10%"></audio>';
                    }
                }
            },
            {
                header: '报修照片', name: 'reportPhoto1', index: 'reportPhoto1', width: 150, align: 'center',
                formatter: function (val, row) {
                    if ($.trim(val) === "") {
                        return ' — ';
                    } else {
                        return '<a target="_blank" href="' + val + '"><img style="height: 30px;" src="' + val + '"/></a>';
                    }
                }
            },
            {header: '是否指派', name: 'assign', index: 'assign', width: 150, align: "center", sortable: false},


            {
                header: '接单人',
                name: 'receiveUserName',
                index: 'receiveUserName',
                width: 100,
                align: "center",
                sortable: false
            },
            {header: '要求到场时间', name: 'requirePresenceTime', index: 'requirePresenceTime', width: 150, align: "center"},
            {header: '要求完成时间', name: 'requireFinishTime', index: 'requireFinishTime', width: 150, align: "center"},
            {header: '维修工单类型', name: 'repairType', index: 'repairType', width: 150, align: "center"},
            {header: '接单时间', name: 'receiveDate', index: 'receiveDate', width: 150, align: "center"},
            {header: '完成时间', name: 'repairFinishTime', index: 'repairFinishTime', width: 150, align: "center"},
            {header: '客户签字时间', name: 'signDate', index: 'signDate', width: 150, align: "center"},
            {header: '报修部门', name: 'organizationName', index: 'organizationName', width: 100, align: "center"},
            {
                header: '是否维修范围',
                name: 'inRoutingScopeName',
                index: 'inRoutingScopeName',
                width: 150,
                align: "center",
                sortable: false
            },
            {header: '接单响应时间(分钟)', name: 'callBack', index: 'callBack', width: 150, align: "center"},

            {
                header: '维修人',
                name: 'receiveUserName',
                index: 'receiveUserName',
                width: 100,
                align: "center",
                sortable: false
            },
            {
                header: '故障分类',
                name: 'troubleCat',
                index: 'troubleCat',
                width: 100,
                align: "center",
                sortable: false
            },
            {header: '未完成理由', name: 'unrepairReason', index: 'unrepairReason', width: 100, align: "center"},
            {
                header: '完成照片', name: 'finishPhoto1', index: 'finishPhoto1', width: 150, align: 'center',
                formatter: function (val, row) {
                    if ($.trim(val) === "") {
                        return ' — ';
                    } else {
                        return '<a target="_blank" href="' + val + '"><img style="height: 30px;" src="' + val + '"/></a>';
                    }
                }
            },
            {header: '接线人', name: 'telephoneUser', index: 'telephoneUser', width: 100, align: "center"},
            {header: '应急措施', name: 'measure', index: 'measure', width: 100, align: "center"},
            {header: '维修到场时间', name: 'presenceTime', index: 'presenceTime', width: 150, align: "center"},
            {
                header: '到场照片', name: 'presencePhoto1', index: 'presencePhoto1', width: 150, align: "center",
                formatter: function (val, row) {
                    if ($.trim(val) === "") {
                        return ' — ';
                    } else {
                        return '<a target="_blank" href="' + val + '"><img style="height: 30px;" src="' + val + '"/></a>';
                    }
                }
            },

            {header: '到场响应时间(分钟)', name: 'callBackPresence', index: 'callBackPresence', width: 150, align: "center"},
            {header: '确认故障人员', name: 'receiveUserName', index: 'receiveUserName', width: 150, align: "center"},
            {header: '评价', name: 'lastRemark', index: 'lastRemark', width: 100, align: "center"},
            // {header: '座机号码', name: 'telephone', index: 'telephone', width: 100, align: "center"},

            {header: '关联设备名称', name: 'facilityName', index: 'facilityName', width: 200, align: "center"},
            {header: '关联设备编号', name: 'facilityCode', index: 'facilityCode', width: 200, align: "center"},
            {
                header: '关联设备专业', name: 'rootCatId', index: 'rootCatId', width: 150, align: 'center',
                formatter: function (cellvalue) {
                    var result;
                    if ($.trim(cellvalue) === "") {
                        result = '';
                    } else {
                        $.ajax({
                            type: 'post',
                            url: "<c:url value="/web/admin/facilityCat/getRootCatNameById.action" />",
                            data: {"id": cellvalue},
                            dataType: 'json',
                            async: false,
                            success: function (data) {
                                result = data.msg;
                            },
                        });
                    }
                    return result;
                }
            },

            {header: '关联设备类型', name: 'facilityCatName', index: 'facilityCatName', width: 100, align: "center"},
            {header: '总工时', name: 'totalWorkingHours', index: 'totalWorkingHours', width: 100, align: "center"},
            {header: '维修人员工时', name: 'workingHours', index: 'workingHours', width: 100, align: "center"},
            {header: '手机号码', name: 'telephone', index: 'telephone', width: 100, align: "center"},

        ],
        // onselectRow:function(id){
        //     var ids = $("#dataGrid").jqGrid("getGridParam", "selarrrow");
        //     if(ids.contains(id)){
        //
        //     }
        // },
        // 加载成功后执行事件
        ajaxSuccess: function (data) {
            //获取这一页所有的id
            var ids = $("#dataGrid").jqGrid("getDataIDs");
            //如果id存在
            if (ids) {
                //循环id
                for (var i = 0; i < ids.length; i++) {
                    //指定id行的数据
                    var rowData = $("#dataGrid").jqGrid("getRowData", ids[i]);
                    //不一致记录标红
                    if (rowData.orderStatus == "待接单") {
                        //插入样式，注意：当一个页面中包含多个jqgrid的时候，addClass的时候不能只根据id来添加，需要在id之前加上tableId '#report_grid_table '
                        $('#dataGrid ' + '#' + ids[i]).find("td").addClass("differentFlag");
                    } else if (rowData.orderStatus == "待到场") {
                        //插入样式，注意：当一个页面中包含多个jqgrid的时候，addClass的时候不能只根据id来添加，需要在id之前加上tableId '#report_grid_table '
                        $('#dataGrid ' + '#' + ids[i]).find("td").addClass("differentFlag1");
                    }
                }
            }

        },
    });
    $("#dataGrid").dataGrid('setFrozenColumns');


    function editRepairOrder(id) {

        vLayuiUtil.openLayerWindow('wRepairOrder', '<c:url value="/web/admin/repairOrder/editPage.action?id="/>' + id, '编辑', ['80%', '95%'])
    }

    function detail(id) {
        vLayuiUtil.openLayerWindow('wRepairOrderDetail', '<c:url value="/web/admin/repairOrder/detail.action?id="/>' + id, '详情', ['80%', '95%'])
    }

    function log(id) {
        vLayuiUtil.openLayerWindow('wRepairOrderLog', '<c:url value="/web/admin/repairOrder/logPage.action?id="/>' + id, '日志', ['50%', '50%'])
    }

    function submitRemove(id) {
        js.confirm('你确认要删除这条数据吗？', function () {
            $.ajax({
                type: 'post',
                url: "<c:url value="/web/admin/repairOrder/delete.action" />",
                data: {"id": id},
                dataType: 'json',
                async: false,
                success: function (data) {
                    js.showMessage(data.msg);
                    $('#dataGrid').dataGrid('refresh');
                },
                error: function () {
                    js.showMessage("出错了，请联系管理员");
                }
            });
        })
    }

    //指定岗位和人员 type:0人员，1：岗位
    function assign(type) {


        var ids = $("#dataGrid").jqGrid("getGridParam", "selarrrow");
        var id = ids[0];
        var ret = $("#dataGrid").jqGrid('getRowData', id);
        // console.log(ret.orderStatus+"测试")
        if (ret.orderStatus != '待接单'){
            js.showMessage("只能指派待接单工单");
            return false;
        }

        if (ids.length == 0) {
            js.showMessage("请选择要派单的维修单");
            return false;
        }




        if (ids.length > 1) {
            js.showMessage("只能选择一条维修单进行派单");
            return false;
        }
        if (type == 0) {
            vLayuiUtil.openLayerIframMaxWindow('wAssignUser', '<c:url value="/web/admin/repairOrder/assignUser.action?repairOrderId="/>' + ids, '指定接单人')
        }
        if (type == 1) {
            vLayuiUtil.openLayerIframMaxWindow('wAssignPosition', '<c:url value="/web/admin/repairOrder/assignPosition.action?repairOrderId="/>' + ids, '指定岗位')
        }

    }


    function downloadData() {
        // ajaxLoading('container1');
        // setTimeout(function () {
        //     $(".datagrid-mask").remove();
        //     $(".datagrid-mask-msg").remove();
        // },2000);
        var reportCode = $('#reportCode').val();
        var orderStatus = $('#orderStatus').val();
        var reportContent = $('#reportContent').val();
        var createDate1 = $('#createDate1').val();
        var createDate2 = $('#createDate2').val();
        var reportType = $('#reportType').val();
        var reportUser = $('#reportUser').val();
        var reportArea = $('#reportArea').val();
        var customer = $('#customerName').val();
        var call = $('#call').val();
        var belongEnterprise = $('#belongEnterprise').val();
        var repairType = $('#repairType').val();
        window.location.href = encodeURI(encodeURI('<c:url value="/web/admin/repairOrder/exportOrder.action" />?reportCode=' + reportCode + '&orderStatus='
            + orderStatus + '&reportContent=' + reportContent + '&createDate1=' + createDate1 + '&createDate2=' + createDate2 + '&reportArea=' + reportArea + '&reportType='
            + reportType + '&reportUser=' + reportUser + '&customer=' + customer + '&callNumber=' + call + '&belongEnterprise=' + belongEnterprise + '&repairType=' + repairType));
    }

    var date = new Date();
    $("#createDate1").datetimepicker({
        format: 'yyyy-mm-dd',
        language: 'zh-CN',
        minView: "month",//设置只显示到月份
        autoclose: true,//选中关闭
        todayBtn: true//今日按钮
    });
    $("#createDate2").datetimepicker({
        format: 'yyyy-mm-dd',
        language: 'zh-CN',
        minView: "month",//设置只显示到月份
        autoclose: true,//选中关闭
        todayBtn: true//今日按钮
    });

    // 时间选择器互动（结束时间不得早于开始时间）
    $("#createDate1").on('changeDate', function (ev) {
        $("#createDate2").datetimepicker('setStartDate', ev.date);
        var d = $("#createDate2").val();
        if (d) {
            var date = new Date(d.replace(/-/g, '/'));
            if (date != 'Invalid Date' && date < ev.date) {
                $("#createDate2").datetimepicker('setDate', ev.date)
            }
        }
    });

</script>
